package com.zf.bm.common.core.sys.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.zf.bm.common.core.shiro.config.ShiroConfig;
import com.zf.bm.common.core.sys.service.SysUserCertService;
import com.zf.bm.common.core.sys.utils.DictUtils;
import com.zf.bm.common.utils.HttpUtils;
import com.zf.bm.common.utils.Result;
import com.zf.bm.common.web.BaseController;
import com.zf.bm.config.ParamConfig;
import com.zf.bm.modules.system.entity.SysInfo;
import com.zf.bm.modules.system.service.SysInfoService;
import com.zf.bm.modules.system.utils.SequenceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "${adminPath}/config/", method = {RequestMethod.GET, RequestMethod.POST})
public class ConfigController extends BaseController {
    private static Map<String, Object> systemConfig = Maps.newHashMap();
    @Value("${sys.project.name}")
    private String systemName;
    @Autowired
    private ShiroConfig config;
    @Autowired
    private SysInfoService sysInfoService;
    @Autowired
    private SequenceUtil sequenceUtil;
    @Resource
    private ParamConfig paramConfig;
    @Resource
    private SysUserCertService sysUserCertService;

    private static final String LICENSE_STATE = "licenseState";
    private static final String VALIDATE_CODE = "validateCode";
    private static final String ZERO = "0";

    @RequestMapping("/list")
    public Result config() {
        QueryWrapper<SysInfo> sysInfoQueryWrapper = new QueryWrapper<>();
        sysInfoQueryWrapper.in("name", "version", "system_title", LICENSE_STATE);
        List<SysInfo> sysInfoList = sysInfoService.list(sysInfoQueryWrapper);
        systemName = sysInfoList.get(1).getValue();
        String version = sysInfoList.get(0).getValue();
        logger.info("系统配置读取,客户端ip:{}", HttpUtils.getRemoteAddr());
        systemConfig.put("systemName", this.systemName);
        systemConfig.put(VALIDATE_CODE, config.isValidateCodeEnable() ? "1" : ZERO);
        systemConfig.put("version", version);
        systemConfig.put(LICENSE_STATE, sysInfoService.getValue("license_status"));
        // 密评开关
        systemConfig.put("keySwitch", paramConfig.getKeySwitch());
        if (Boolean.TRUE.equals(paramConfig.getKeySwitch())) {
            systemConfig.put("certList", sysUserCertService.findCertNameList());
            systemConfig.put("secretType", paramConfig.getType());
        }

        // 自动化测试开关，on：开；off：关，默认关
        if (DictUtils.isAutoTest()) {
            systemConfig.put(VALIDATE_CODE, ZERO);
            systemConfig.put(LICENSE_STATE, ZERO);
        }
        return new Result(systemConfig);
    }


    /**
     * 获取系统时间
     *
     * @return
     */
    @RequestMapping("/date")
    public Result getSystemDate() {
        Map<String, Object> data = new HashMap<>();
        data.put("date", new Date());
        return new Result(data);
    }

}
